<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Survey_model extends CI_Model {

  //var $table = 'ksv_surveys';
   public function __construct()
   {
    parent::__construct();
   }

  //hiển thị dữ liệu project theo user tương ứng
  public function displayProject($userId){
    $sql = 'select user_id, survey_name from survey where user_id = :userid';
    $stm = $this->db->prepare($sql);
    $stm->bindValue(':userid',$userId);
    $stm->execute();
    $rs = $stm->fetchAll(PDO::FETCH_ASSOC);
    $stm->closeCursor();
    return $rs;

  }
  public function insertProject($arr){
    $key = array();
    $value = array();
    foreach ($arr as $k => $v) {
        $key[] = $k;
        $value[] = $v;
      }  
    $sql = "INSERT INTO survey (".$key[0].",".$key[1].",".$key[2].",".$key[3].",".$key[4].",".$key[5].") VALUES ('".$value[0]."','".$value[1]."','".$value[2]."','".$value[3]."','".$value[4]."','".$value[5]."')";
    //echo $sql;die();
    $stmt = $this->db->prepare($sql);
    $stmt->execute();
    $id = $this->db->lastInsertId();
    $stmt->closeCursor();
    return $id;

  }

  //Lấy thông tin các dự án của user
  public function projectInfo($userId){
    $sql = 'select * from (survey inner join auth_users on survey.user_id = auth_users.user_id) inner join survey_type on survey_type.type_id = survey.type where survey.user_id = :id';
    $stmt = $this->db->prepare($sql);
    $stmt->bindValue(':id',$userId);
    $stmt->execute();
    $rs = $stmt->fetchAll(PDO::FETCH_ASSOC);
    return $rs;
  }

  //update link cho project
  public function updateId($id){
    $sql = "update survey set survey_link = 'tao-cau-hoi-khao-sat/".$id."' where survey_id = '".$id."'";
    $stmt = $this->db->prepare($sql);
    $stmt->execute(); 
    $stmt->closeCursor();
  }

  //delete project
  public function delProject($id){
    //the first, delete all question of survey
    $sql ="delete from question where question_survey = ".$id." and question.question_id = question_survey.question_id";

    //$sql = "delete from survey where survey_id = '".$id."'";
    $stmt = $this->db->prepare($sql);
    $stmt->execute();
    $stmt->closeCursor();
  }
  //Edit project
  public function editProject($id,$n){
    $sql = "update survey set survey_name ='".$n."' where survey_id = '".$id."'";
    //echo $sql;die();
    $stmt = $this->db->prepare($sql);
    $stmt->execute();
    $stmt->closeCursor(); 
  }
  //get info project by project ID
  public function getInfoByProId($surveyId){
    $sql = "select * from survey where survey_id = :id";
    $stmt = $this->db->prepare($sql);
    $stmt->bindValue(':id',$surveyId);
    $stmt->execute();
    $rs = $stmt->fetchAll(PDO::FETCH_ASSOC);
    $stmt->closeCursor();
    return $rs;
  }
  
   //get info all question by project ID
  public function getInfoQuesByProId($projectId){
    $sql = "select question_survey.survey_id, question.question_id, random, title, answer, answer_type,sort from question inner join question_survey on question.question_id = question_survey.question_id where question_survey.survey_id = :id";
    $stmt = $this->db->prepare($sql);
    $stmt->bindValue(':id',$projectId);
     $stmt->execute();
    $rs = $stmt->fetchAll(PDO::FETCH_ASSOC);
    $stmt->closeCursor();
    return $rs;
  }

   //get info a question by project ID
  public function getInfoOneQuesByProId($projectId,$questionId){
    $sql = "select question_survey.survey_id, question.question_id, random, title, answer, answer_type,sort 
            from question inner join question_survey on question.question_id = question_survey.question_id 
            where question_survey.survey_id = :idsur";
    $stmt = $this->db->prepare($sql);
    $stmt->bindValue(':idsur',$projectId);
    //$stmt->bindValue(':idques',$questionId);
     $stmt->execute();
    $rs = $stmt->fetchAll(PDO::FETCH_ASSOC);
    $stmt->closeCursor();
    return $rs;
  }

  //Count Question by Id Survey
  public function countQuesByIdSur($surveyId){
      //Count question by survey_id
    $sql = "select question_id from question_survey where survey_id = '".$surveyId."'";
    $stmt = $this->db->prepare($sql);
    $stmt->execute();
    $count = $stmt->rowCount();
    $stmt->closeCursor();
    return $count+1;
  }
//Insert Question_Survey
  public function insertQuestionSurvey($surveyId,$numQ){
    //Insert question
    $sql = "insert into question_survey (survey_id, question_id) values('".$surveyId."', '".$numQ."')";
     $stmt = $this->db->prepare($sql);
     $stmt->execute();
      $stmt->closeCursor();
  }
  public function insertQuestion($countQ,$questionTitle,$questionContent,$questionType){
     $sql = "insert into question(question_id, random, title, answer, answer_type, created_time, user_id) values('".$countQ."','1','".$questionTitle."','".$questionContent."','".$questionType."','".date("d-m-y, h:i:s")."','".$this->session->userdata('user_id')."')";
     $stmt = $this->db->prepare($sql);
     $stmt->execute();
      $stmt->closeCursor();
      return $sql;
  }

  function updateSurvey($isSurvey, $isUser, $dataSurvey) {
    $sql = "UPDATE `survey` SET theme_id='".$dataSurvey['theme_id']."' WHERE survey_id=".$isSurvey." AND user_id=".$isUser;
    $stmt = $this->db->prepare($sql);
    $stmt->execute();
  }
  /*
   //get info question by project ID and question id
  public function getInfoQuesByProIdAndQuesId($projectId, $questionId){
    $this->db->select('ksv_questions.*, ksv_projects.projectName');
    $this->db->from('ksv_questions');
    $this->db->join('ksv_projects','ksv_projects.projectId = ksv_questions.projectId');
    $this->db->where('ksv_questions.projectId',$projectId);
    $this->db->where('ksv_questions.questionId',$questionId);
    return $this->db->get()->result_array();
  }
  //Hàm chèn dữ liệu sau khi người dùng tạo câu hỏi
  public function insert($tab,$arr)
  {  
    $this->db->insert($tab, $arr); 
    return $this->db->insert_id();
  }
  //update sort
  public function updateSort($set,$cond)
  {
    $data = array('sort'=>$set);
    $this->db->where('questionOfProject', $cond);
    $this->db->update('ksv_projects', $data); 
  }*/
  //Delete question
  public function delQuestion($idQues, $idPro){
    $sql = "delete a.*, b.* from question_survey as a, question as b where a.question_id = b.question_id and b.question_id = :idQues and a.survey_id = :idPro";

    $stmt = $this->db->prepare($sql);
    $stmt->bindValue(':idQues',$idQues);
    $stmt->bindValue(':idPro',$idPro);
    $stmt->execute();

    $stmt->closeCursor();
  }
/*
  public function getQuestionById($id){
     $this->db->select('*');
    $this->db->from('ksv_questions');
    $this->db->where('questionId',$id);
    return $this->db->get()->result_array();
  }
  //Count question of project to insert sort field
  public function countQuestion($id){
    $this->db->select('projectId');
      $this->db->from('ksv_questions');
      $this->db->where('projectId',$id);
    
      return $this->db->get()->num_rows();
  }
  //get sort values
  public function listSortQuestion($id){
    $this->db->select('sort');
      $this->db->from('ksv_questions');
      $this->db->where('projectId',$id);
     return $this->db->get()->result_array();
  }
  //edit question
  public function showQuestionById($id){
      $this->db->select('*');
      $this->db->from('ksv_questions');
      $this->db->where('questionId',$id);
     return $this->db->get()->result_array();
  }
  //update question content when add selection
  public function updateContentQuestion($id,$title,$content){
    $data = array('questionTitle'=>$title,'questionContent'=>$content);
    $this->db->where('questionId', $id);
    $this->db->update('ksv_questions', $data); 
  }*/
}
?>